package com.hyts.mvc.loader.item;

import com.hyts.mvc.bean.MvcConfigBean;
import com.hyts.mvc.config.MvcKey;
import com.hyts.mvc.config.MvcValue;
import com.hyts.mvc.config.ViewType;
import com.hyts.mvc.loader.WebMvcConfigLoader;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import javax.servlet.ServletConfig;

/**
 * project-name:coffice-toolbox
 * package-name:com.hyts.mvc.loader.item
 * author:Libo/Alex
 * create-date:2019-07-01 11:00
 * email:liboware@gmail.com
 * description:此类主要用于: 读取servlet模式的配置服务
 */
@Slf4j
public class WebMvcServletConfigLoader extends WebMvcConfigLoader<ServletConfig>{


    /**
     * 加载信息mvc操作配置
     * @param param
     * @return
     */
    @Override
    public MvcConfigBean load(ServletConfig param,MvcConfigBean mvcConfigBean) {
        //目前暂时不开发动态选择view视图解析器
        mvcConfigBean.setViewType(ViewType.FREEMARKER);
        mvcConfigBean.setScanPackage(StringUtils.defaultIfBlank(param.getInitParameter
                (MvcKey.SCAN_MVC_PACKAGE_KEY),"").split(MvcValue.SCAN_COMPONENT_PACKAGE_SPLIT_TOKEN[0]));
        log.info("读取到参数:{} - 值:{}", MvcKey.SCAN_MVC_PACKAGE_KEY,mvcConfigBean.getScanPackage());
        //servlet定义模式暂时无用
        mvcConfigBean.setUrlPath(MvcValue.DEFAULT_VIEW_SERVLET_URL_PATH);
        //请求转发的view转发器配置-prefix
        mvcConfigBean.setPrefixPath(
                StringUtils.defaultIfBlank(param.getInitParameter(MvcKey.MVC_VIEW_PREFIX_PATH), MvcValue.DEFAULT_VIEW_PREFIX_PATH));
        log.info("读取到参数:{} - 值:{}", MvcKey.MVC_VIEW_PREFIX_PATH,mvcConfigBean.getPrefixPath());
        //请求转发的view转发器配置-subfix
        mvcConfigBean.setSubfixPath(
                StringUtils.defaultIfBlank(param.getInitParameter(MvcKey.MVC_VIEW_SUBFIX_PATH), MvcValue.DEFAULT_VIEW_SUBFIX_PATH));
        log.info("读取到参数:{} - 值:{}", MvcKey.MVC_VIEW_SUBFIX_PATH,mvcConfigBean.getSubfixPath());
        return mvcConfigBean;
    }

    @Override
    public int order() {
        return 2;
    }
}
